//
//  PersonalSettingBlockController.m
//  ChatForHot
//
//  Created by wr on 2018/10/17.
//  Copyright © 2018年 FLY. All rights reserved.
//

#import "PersonalSettingBlockController.h"
#import "PersonalSettingViewModel.h"
#import "PersonalBlockCell.h"
#import "MJRefresh.h"
#import "UserInfoModel.h"
#import "BaseTableView.h"
#import <Hyphenate/Hyphenate.h>

@interface PersonalSettingBlockController ()<UITableViewDataSource,UITableViewDelegate,DeleteBlockDelegate>

@property (nonatomic, strong) PersonalSettingViewModel *viewModel;
@property (nonatomic, strong) BaseTableView *HOTCHATtableView;
@property (nonatomic, assign) NSInteger currentPage;

@end

@implementation PersonalSettingBlockController

- (void)HOTCHATdidInitialized {
    [super HOTCHATdidInitialized];
    
    self.viewModel = [[PersonalSettingViewModel alloc] init];
}

- (void)HOTCHATInsertSubViews {
    [super HOTCHATInsertSubViews];
    self.HOTCHATtableView = [[BaseTableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.HOTCHATtableView.delegate = self;
    self.HOTCHATtableView.dataSource = self;
    self.HOTCHATtableView.backgroundColor = HOTCHATrgb(247, 247, 247);
    self.HOTCHATtableView.rowHeight = 65;
    [self.HOTCHATtableView registerClass:[PersonalBlockCell class] forCellReuseIdentifier:@"PersonalBlockCell"];
    [self.view addSubview:self.HOTCHATtableView];
    
    MJRefreshNormalHeader *refreshHeader = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.HOTCHATtableView.mj_footer endRefreshing];
        NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
        self.currentPage = 1;
        [dic setValue:[APPINFO defaultManager].accountid forKey:@"shieldingAccountid"];
        [dic setValue:[NSNumber numberWithInteger:self.currentPage] forKey:@"currentPage"];
        [self.viewModel.requestBlockCommand execute:dic];
    }];
    refreshHeader.stateLabel.hidden = YES;
    refreshHeader.lastUpdatedTimeLabel.hidden = YES;
    self.HOTCHATtableView.mj_header = refreshHeader;
    
    MJRefreshAutoNormalFooter * refreshFooter= [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self.HOTCHATtableView.mj_header endRefreshing];
        NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
        [dic setValue:[APPINFO defaultManager].accountid forKey:@"shieldingAccountid"];
        [dic setValue:[NSNumber numberWithInteger:self.currentPage] forKey:@"currentPage"];
        [self.viewModel.requestBlockCommand execute:dic];
    }];
    refreshFooter.stateLabel.hidden = YES;
    refreshFooter.refreshingTitleHidden = YES;
    self.HOTCHATtableView.mj_footer = refreshFooter;
}

- (void)HOTCHATsetNavigationItemsIsInEditMode:(BOOL)isInEditMode animated:(BOOL)animated {
    [super HOTCHATsetNavigationItemsIsInEditMode:isInEditMode animated:animated];
    
    self.title = NSLocalizedString(@"personS2", nil);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    @weakify(self)
    
    [self.viewModel.requestBlockCommand.executionSignals subscribeNext:^(RACSignal *excution) {
        [[excution dematerialize] subscribeNext:^(id x) {
            @strongify(self)
            self.currentPage ++;
            if (self.viewModel.blockDataArray.count == 0) {
                [self.HOTCHATtableView showEmptyViewWithImage:[UIImage imageNamed:@"m_ic_no_message"]];
            }else {
                [self.HOTCHATtableView hideEmptyView];
            }
            [self.HOTCHATtableView.mj_header endRefreshing];
            [self.HOTCHATtableView.mj_footer endRefreshing];
            [self.HOTCHATtableView reloadData];
            [self HOTCHATstopLoading];
        } error:^(NSError *error) {
            @strongify(self)
            [self HOTCHATstopLoading];
            [self.HOTCHATtableView.mj_header endRefreshing];
            [self.HOTCHATtableView.mj_footer endRefreshing];
        }];
    }];
    
    [self.viewModel.requestDeleteBlockCommand.executionSignals subscribeNext:^(RACSignal *excution) {
        [[excution dematerialize] subscribeNext:^(id x) {
            @strongify(self)
            if (self.viewModel.blockDataArray.count == 0) {
                [self.HOTCHATtableView showEmptyViewWithImage:[UIImage imageNamed:@"m_ic_no_message"]];
            }else {
                [self.HOTCHATtableView hideEmptyView];
            }
        } error:^(NSError *error) {
            //            @strongify(self)
        }];
    }];
    
    [self HOTCHATstartLoading];
    [self.HOTCHATtableView.mj_header beginRefreshing];
}

#pragma mark - UITableViewDataSource,UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.viewModel.blockDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    PersonalBlockCell *cell = [tableView dequeueReusableCellWithIdentifier:@"PersonalBlockCell"];
    [cell bind:[self.viewModel.blockDataArray objectOrNilAtIndex:indexPath.row]];
    cell.delegate = self;
    cell.HOTCHATIndexPath = indexPath;
    return cell;
}

- (void)HOTCHATDeleteBlockCell:(UserInfoModel *)model atIndex:(NSIndexPath *)indexpath {
    NSInteger index = [self.viewModel.blockDataArray indexOfObject:model];
    [[EMClient sharedClient].contactManager removeUserFromBlackList:model.IMUser completion:^(NSString *aUsername, EMError *aError) {
        if (!aError) {
            NSLog(@"发送成功");
        }
    }];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];
    [dic setValue:[APPINFO defaultManager].accountid forKey:@"shieldingAccountid"];
    [dic setValue:model.accountid forKey:@"beShieldingAccountid"];
    [self.viewModel.requestDeleteBlockCommand execute:dic];
    [self.viewModel.blockDataArray removeObject:model];
    
    [self.HOTCHATtableView beginUpdates];
    NSIndexPath *indexpp = [NSIndexPath indexPathForRow:index inSection:0];
    [self.HOTCHATtableView deleteRowsAtIndexPaths:@[indexpp] withRowAnimation:UITableViewRowAnimationFade];
    [self.HOTCHATtableView endUpdates];
}

@end
